Repository navigation
fix(android): Stop the Hermes sampler before the JS thread exits on reload - #7157
Merged
Merged
Conversation
…eload A reload during a Hermes profile could abort the app with `invalid pthread_t passed to pthread_kill` on the hermes-sampling thread (#5441, meta/hermes#1853). The fix from #5441 disables the sampler in invalidate(), but bridgeless ReactInstance.destroy() quits and joins the JS thread before it invalidates the native modules, so on the New Architecture invalidate() runs too late. ReactHost.addBeforeDestroyListener, from React Native 0.73, runs on reload and on destroy while the JS thread still runs. In bridgeless apps the module now registers a listener there that runs the invalidate() cleanup. invalidate() stays as the cleanup on the bridge, where it runs before the JS thread exits, and unregisters the listener, so the long-lived React host does not keep a destroyed module. A profile cannot start after the cleanup, also when invalidate() runs between the check and the start. The SDK compiles against the app's React Native version, so the listener reaches the React host through reflection. R8 removed addBeforeDestroyListener from minified release builds, because React Native does not call it, so consumer R8 rules keep these members. On an Android emulator with a release build, profilesSampleRate 1.0, and reactHost.reload() every 1.5 s, main aborted 2 times in 230 reloads, and this change aborted 0 times in 300 reloads.
Replace the PR link placeholder with #7157.
Contributor
Semver Impact of This PR⚪ None (no version bump detected) 📋 Changelog PreviewThis is how your changes will appear in the changelog.
🤖 This preview updates automatically when you update the PR. |
Contributor
PR risk: mediumFor commit Main concerns: host performance, public contract, reach. Rules that block low also fired:
This label is a hint for reviewers, not an approval. |
alwx
marked this pull request as ready for review
December 1, 2026 10:49
alwx
added a commit
that referenced
this pull request
Dec 1, 2026
…is destroyed Review feedback on #7111. On the New Architecture, the before destroy listener from #7157 runs invalidate(), which stops the JS hang tracker while the JS thread still runs. A start that came in after that cleanup would leave the tracker, and with it the Hermes sampler, running while the JS thread exits. startJsHangTracking now does nothing after the cleanup, and stops the tracker again when the cleanup ran between the check and the start. With reactHost.reload() every 1.5 s in a release build with profilesSampleRate 1.0, 600 reloads gave no abort on the hermes-sampling thread.
6 of 11 tasks
📲 Install BuildsAndroid
|
antonis
approved these changes
Dec 1, 2026
antonis
left a comment
Contributor
There was a problem hiding this comment.
LGTM once the CI is 🟢 🚀
Thanks for the quick turnaround.
alwx
added a commit
that referenced
this pull request
Dec 1, 2026
…is destroyed Review feedback on #7111. On the New Architecture, the before destroy listener from #7157 runs invalidate(), which stops the JS hang tracker while the JS thread still runs. A start that came in after that cleanup would leave the tracker, and with it the Hermes sampler, running while the JS thread exits. startJsHangTracking now does nothing after the cleanup, and stops the tracker again when the cleanup ran between the check and the start. With reactHost.reload() every 1.5 s in a release build with profilesSampleRate 1.0, 600 reloads gave no abort on the hermes-sampling thread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📢 Type of change
📜 Description
On bridgeless Android apps (React Native 0.73+), the module now runs its Hermes sampler cleanup from
ReactHost.addBeforeDestroyListener, which runs on reload and destroy while the JS thread still runs.invalidate()stays as the cleanup on the bridge and unregisters the listener. A profile cannot start after the cleanup.The SDK compiles against the app's React Native version, so the listener reaches the React host through reflection. R8 removed
addBeforeDestroyListenerfrom minified release builds, so the module now ships consumer R8 rules (android/proguard-rules.pro) that keep these members.💡 Motivation and Context
Follow-up to #5441, found by @antonis in #7111. The #5441 fix disables the sampler in
invalidate(), but bridgelessReactInstance.destroy()quits and joins the JS thread before it invalidates the native modules (ReactInstance.kt#L419-L422), so a reload during a profile can still abort withinvalid pthread_t passed to pthread_killonhermes-sampling(meta/hermes#1853). #7111 will be rebased on top of this.💚 How did you test it?
Robolectric tests for the listener: registers only bridgeless apps with a React host, runs the action on destroy, unregisters (also from inside the listener), and never lets the action throw into React Native.
testDebugUnitTestpasses (174 tests).yarn build,yarn test,yarn lint, andyarn circularDepCheckpass.Reload loop on an Android 16 emulator: release arm64 build of the sample,
profilesSampleRate: 1.0,reactHost.reload()fromadbevery 1.5 s through a temporary receiver.hermes-samplingmain📝 Checklist
sendDefaultPIIis enabled.